package com.sf.fy.uav.applet.operate.controller;

import com.sf.fy.uav.common.enums.CouponTypeEnum;
import com.sf.fy.uav.common.response.operational.EffectiveCouponResponse;
import com.sf.fy.uav.common.response.operational.ExpireOrUsedUserRsCouponResponse;
import com.sf.fy.uav.common.response.operational.UserCouponRsResponse;
import com.sf.fy.uav.common.response.order.CouponPriceResponse;
import com.sf.fy.uav.common.utils.BeanUtil;
import com.sf.fy.uav.common.utils.Rs;
import com.sf.fy.uav.controller.UserCouponRest;
import com.sf.fy.uav.entity.Coupon;
import com.sf.fy.uav.order.entity.ExpenseDetails;
import com.sf.fy.uav.order.mapper.ExpenseDetailsMapper;
import com.sf.fy.uav.rest.CouponRest;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import scala.collection.parallel.immutable.ParRange;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 01430312
 */
@RestController
@RequestMapping("/sys/coupon/")
public class SysCouponController {

    @Autowired
    private UserCouponRest userCouponRest;

    @Autowired
    private CouponRest couponRest;

    @Autowired
    private ExpenseDetailsMapper expenseDetailsMapper;

    @GetMapping("available")
    @Operation(tags = "获取可用优惠券", description = "获取可用优惠券")
    public Rs<UserCouponRsResponse> getAvailableCoupons(@RequestParam Integer page, @RequestParam Integer size, @RequestParam(required = false) BigDecimal price, @RequestParam Integer isMustStart) {
        return Rs.success(userCouponRest.getAvailableCoupons(page, size, price, isMustStart));
    }

    @GetMapping("unavailable")
    @Operation(tags = "获取不可用优惠券", description = "获取不可用优惠券")
    public Rs<UserCouponRsResponse> getUnavailableCoupons(@RequestParam Integer page, @RequestParam Integer size, BigDecimal price) {
        return Rs.success(userCouponRest.getUnavailableCoupons(page, size, price));
    }

    @GetMapping("calculate-price")
    @Operation(tags = "计算价格", description = "计算价格")
    public Rs<CouponPriceResponse> calculatePrice(@RequestParam BigDecimal price, @RequestParam String id, @RequestParam(required = false) String expenseDetailsId) {
        CouponPriceResponse couponPriceResponse = userCouponRest.calculatePrice(price, id);
        BigDecimal rsPrice = couponPriceResponse.getPrice();
        BigDecimal rsReductionPrice = couponPriceResponse.getReductionPrice();
        if (!ObjectUtils.isEmpty(expenseDetailsId)) {
            ExpenseDetails expendDetails = expenseDetailsMapper.selectById(expenseDetailsId);
            if (!ObjectUtils.isEmpty(expendDetails)) {
                if (price.compareTo(rsPrice.add(rsReductionPrice)) != 0) {
                    throw new RuntimeException("价格异常，请联系客服");
                }
                expendDetails.setDiscount(couponPriceResponse.getDiscount());
                expendDetails.setCouponType(couponPriceResponse.getCouponType());
                expendDetails.setPrice(rsPrice);
                expendDetails.setReductionPrice(rsReductionPrice);
                expendDetails.setUpdateTime(new Date());
                expenseDetailsMapper.updateById(expendDetails);
            }

        }
        return Rs.success(couponPriceResponse);
    }

    @GetMapping("expire-or-used")
    @Operation(tags = "获取过期或者已使用优惠券", description = "获取过期或者已使用优惠券")
    public Rs<ExpireOrUsedUserRsCouponResponse> getExpireOrUsedCoupons(@RequestParam Integer page, @RequestParam Integer size) {
        return Rs.success(userCouponRest.getExpireOrUsedCoupons(page, size));
    }



}
